package com.pozu.new_retail.controllers;

import com.google.gson.Gson;
import com.pozu.new_retail.dao.base.JsonResult;
import com.pozu.new_retail.dao.model.AppSetting;
import com.pozu.new_retail.dao.model.User;
import com.pozu.new_retail.dao.model.WXLoginDomain;
import com.pozu.new_retail.service.AppSettingService;
import com.pozu.new_retail.service.UserService;
import com.pozu.new_retail.utils.CodeConstants;
import com.pozu.new_retail.utils.CodeProperties;
import com.pozu.new_retail.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.Map;

import static com.pozu.new_retail.utils.HttpUtil.doGet;

@Controller
public class LoginController {

    @Autowired
    private AppSettingService appSettingService;

    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "/api/login", method = RequestMethod.POST)
    public Object login(HttpServletRequest request) throws Exception{
        Integer appId = Integer.parseInt(request.getHeader("appId"));
        AppSetting appSetting = appSettingService.queryByAppId(appId);
        Map<String, Object> map = Tools.getRequestData(request);
        //获取用户的openId
        String js_code = map.get("code").toString();
        String httpUrl = "https://api.weixin.qq.com/sns/jscode2session?appid="+appSetting.getWxAppId()+"&secret="+appSetting.getAppSecret()+"&js_code="+js_code+"&grant_type=authorization_code";
        System.out.print("请求微信地址:"+httpUrl);
        String requestReslut = doGet(httpUrl);
        WXLoginDomain wxResult = new Gson().fromJson(requestReslut, WXLoginDomain.class);
        System.out.print("请求结果:"+requestReslut);

        JsonResult jr = new JsonResult();
        // 如果code校验成功
        if (wxResult.getErrcode() == 0) {
            //2、根据openid 去数据库查找userid
            User user = userService.queryByOpenId(wxResult.getOpenid());
            if (user != null && user.getUserId() > 0){
                //已经存在用户了
                user.setWxSession_key(wxResult.getSession_key());
                userService.updateUser(user);
                jr.respData = user;
            }else{
                User newUser = new User();
                //需要创建用户
                newUser.setWxOpenId(wxResult.getOpenid());
                newUser.setWxSession_key(wxResult.getSession_key());
                newUser.setNickName("匿名人士");
                newUser.setChannelId(1);
                newUser.setAppId(appId);
                userService.saveUser(newUser);
                jr.respData = userService.queryByOpenId(wxResult.getOpenid());
            }
            jr.respCode = CodeConstants.SUCCESS_CODE;
            jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
        } else {
            jr.respCode = CodeConstants.USERINFOLOGINERRER_MSG;
            jr.respMsg = CodeProperties.getString(CodeConstants.USERINFOLOGINERRER_MSG);
        }
        return jr;
    }
}
